package com.study.spaoplog.controller;

import com.study.spaoplog.bean.SysLog;
import com.study.spaoplog.constant.SysConst;
import com.study.spaoplog.service.HelloService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * @author MI
 * @version 1.0
 * @date 2021/6/4 21:42
 */
@Slf4j
@RestController
@RequestMapping("/hello")
@Api(tags = "测试-hello")
public class HelloController {
    @Autowired
    private HelloService helloService;

    @GetMapping("/date")
    public LocalDateTime date() {
        return LocalDateTime.now();
    }

    @GetMapping("/time")
    public /*final*/ String dateStr() {
        log.info("HelloController.dateStr");
        return helloService.dateStr();
    }

    @ApiOperation(value = "测试用例", notes = "详细描述-测试用例")
    @GetMapping({"/{sys}"})
    public List<SysLog> followList(@ApiParam(value = "测试用例字符串", required = true)
                                   @PathVariable("sys") String sysName) {
        return Arrays.asList(new SysLog(sysName));
    }

    @ApiOperation(value = "随机返回字符串", notes = "详细描述-randomAlphabetic")
    @GetMapping("rand")
    public String randStr(
            @ApiParam(name = "uid", value = "分类ID", required = true) @RequestParam("uid") String uid,
            @ApiParam(value = "名称", required = true) @RequestParam String uName,
            @RequestParam(defaultValue = SysConst.STR_LOCK, required = false) String strLock
    ) {
        System.out.println(uid + "-" + uName);
        final String randStr = helloService.randStr(uid);
        return randStr;
    }

    @ApiOperation(value = "随机返回字符串", notes = "详细描述-randomAlphabetic")
    @PostMapping("rand")
    public String randStrPost(@RequestBody SysLog sysLog) {
        System.out.println(sysLog);
        final String randStr = helloService.randStr(sysLog);
        return randStr;
    }

    @ApiOperation(value = "退出-exit", notes = "System.exit(0)")
    @GetMapping("exit")
    public String exit() {
        System.exit(0);
        return RandomStringUtils.randomAlphabetic(5);
    }

    /**
     * <p>自调用的时也走切面逻</p>
     * <p>1 自装配</p>
     * <p>2 让自调用走代理的方法2：启用 exposeProxy</p>
     */
    @ApiOperation(value = "AOP自调用测试", notes = "详细描述-测试安排AOP自动用不走代理")
    @GetMapping("aop")
    public String aopTest() {
        // 获取当前代理
        HelloController target = (HelloController) AopContext.currentProxy();
        return target.aopRandStr();
    }

    public String aopRandStr() {
        return RandomStringUtils.randomAlphabetic(5);
    }
}
